/*****************************************************************************************
**  Author:jxx 2022
**  QQ:283591387
**完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
**常用示例见：http://v2.volcore.xyz/document/vueDev
**后台操作见：http://v2.volcore.xyz/document/netCoreDev
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import gridHeader from "@/components/print/selectTemplet.vue"
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader,
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    //下面这些方法可以保留也可以删除
    async onInit() {
      this.queryFields = [ "Client","Return_date_required"]
      this.boxOptions.labelWidth = 140;
      let startTime = this.base.getOneWeekAgoDate()
      let endTime = this.base.getDate(false) + " 23:59:59"
      this.searchFormFields.Return_date_required = [startTime, endTime]
      this.detailOptions.textInline = false
      this.textInline = false

      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == 'Attachment' || item.field == 'Image') {
            item.multiple = true;
            item.maxFile = 10;
          }
        })
      })
      if (this.$route.path == "/q_aftersale" && JSON.stringify(this.$route.query) != '{}') {
        console.log("触发查询")
        await this.base.setSearchParamFn(this)
        // this.searchFormFields.Voucher_sub = null
        // this.searchFormFields.Project_name = null
        // this.searchFormFields.Client = null
        // this.searchFormFields.Client_man = null
        // this.searchFormFields.Project_class = null
        // this.searchFormFields.Service_clsaa = null
        // this.searchFormFields.AuditStatus = null
        // this.searchFormFields.Return_date_required = [null, null]
        // const selectList = ['Voucher_sub','Project_name','Client_man',]
        // this.searchFormOptions.forEach(item => {
        //   item.forEach(x => {
        //     if (!selectList.includes(x.field)) {
        //       x.onChange = (v) => {
        //         this.$route.query = {}
        //       }
        //     }
        //   })
        // })
        setTimeout(() => {
          this.refresh()
        })
      }
    },
    async onInited() {
      this.single = true
      if (this.$route.path != "/q_aftersale") {
        this.setFiexdSearchForm(true);
        this.height = 550 - 150;
        this.buttons.forEach(item => {
          if (item.value != "Search" ) {
            item.hidden = true
          }
        })
        this.searchFormFields.AuditStatus = "1"

        // if (this.$route.path == "/q_aftersale_notarize") {
        //   this.searchFormFields.Contract_state = "1";
        //   this.searchFormFields.Maintain_state = "2";
        // } else {
        //   this.searchFormFields.Contract_state = "0";
        //   this.searchFormFields.Maintain_state = "0";
        // }
      }
      // await this.setFiexdSearchForm(true);
      // await this.buttons.forEach(item => {
      //   if (item.name == "高级查询") {
      //     item.hidden = true
      //   }
      // })
    },
    async  searchBefore(param) {
      
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      if (this.$route.path != "/q_aftersale") {
        param.wheres = param.wheres.filter(item => item.name != "Aftersale")
        this.$route.query = {}
      }
      if (this.$route.path == "/q_aftersale" && JSON.stringify(this.$route.query) != '{}') {
        await this.base.setSearchBefore(this, "Aftersale",param)
        // const sectionState = ["[null,null]", JSON.stringify(["", null]), JSON.stringify([null, ""]), JSON.stringify(["", ""])]
        // const dateState = sectionState.includes(JSON.stringify(this.searchFormFields.Return_date_required))
        // if (this.searchFormFields.Voucher_sub || this.searchFormFields.Project_name || this.searchFormFields.Client_man || !dateState) {
        //   param.wheres = param.wheres.filter(item => item.name != "Aftersale")
        //   this.$route.query = {}
        // }
      }
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      result.forEach(item => {
        if (item.Servicing_time !== null) {
          item.Servicing_time += "天"
        }
      })
      return true;
    },
    delBefore(ids, rows) { //查询界面的表删除前 ids为删除的id数组,rows删除的行
      let auditStatus = rows.some(x => { return x.AuditStatus == 1 });
      if (auditStatus) {
        this.base.ElMessageFn('不支持删除审核通过数据')
        return false;
      }
      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return true;
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true;
    },
    async addBeforeAsync(formData) {
      formData.mainData.Contract_state = "0"
      formData.mainData.Maintain_state = "0"
      return true
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      this.$refs.table.$refs.table.toggleRowSelection(row);
    },
    async modelOpenBefore(row) {
      this.CreateUserBoole = row.CreateID == this.$store.state.userID
      this.rowAuditStatus = await row.AuditStatus
      if (JSON.stringify(row) == "{}") {
        this.rowAuditStatus = 0
        this.CreateUserBoole = true
      }
      if (this.$route.path != "/q_aftersale" || this.rowAuditStatus != 0) {//|| !this.CreateUserBoole
        this.boxButtons[0].hidden = true
      } else {
        this.boxButtons[0].hidden = false
      }
    },
    modelOpenAfter(row) {
      if (this.currentAction!='Add'&&row.Servicing_time !== null) {
        this.editFormFields.Servicing_time = +row.Servicing_time.replace(/天/g, "");
      }
      this.editFormOptions.forEach(item => {
        item.forEach(x => {
          if (x.field == 'Client') {
            x.onChange = (e) => {
              this.editFormFields.Client_man = null;
              this.editFormFields.Client_tel = null;
              console.log("e", e)
              let url = `api/q_client_f/GetClientInfo?bm=` + e;
              this.http
                .post(url, {})
                .then(([res]) => {
                  // console.log("res",res)
                  this.editFormFields.Client_man = res.Client_man;
                  this.editFormFields.Client_tel = res.Client_tel;
                })
                .catch(() => {
                  this.base.ElMessageFn('获取客户信息失败', "error")
                });
            }
          }
          if (x.field == "Contract_state" || x.field == "Maintain_state") {
            x.hidden = true
          }
          if (x.field == "Voucher_sub") {
            x.disabled = true
          } else {
            x.disabled = false
          }
          if (this.$route.path != "/q_aftersale" || this.rowAuditStatus != 0) {//|| !this.CreateUserBoole
            x.disabled = true
          }
        })
      })
    },
  }
};
export default extension;
